package cn.northsea.controller;

import cn.northsea.pojo.Menu;
import cn.northsea.service.MenuService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping("/toAllocationMenu")
    public String toAllocationRole(Integer pageNum, Integer role_id, Model model) {
        List<Menu> menuList = menuService.findAll();
        List<Menu> menus = menuService.findMenuByRid(role_id);
        model.addAttribute("menus", JSON.toJSONString(menus));
        model.addAttribute("menuList", menuList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("role_id", role_id);
        return "allocationMenu";
    }

    @RequestMapping("/allocationMenu")
    public String allocationMenu(Integer pageNum, Integer role_id, @RequestParam(name = "mids") List<Integer> mids, Model model) {
        menuService.allocationMenu(role_id, mids);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/role/findAll.do";
    }

}
